{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ch08"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "# 8.1 调用不需要变量的函数\n",
    "\n",
    "# 8.1.1 变量 def f(x), p(x), 调用的时候，f(a)\n",
    "\n",
    "\n",
    "def greet_user(username):\n",
    "    \"\"\"显示简单的问候语\"\"\" ## 注释问题，看一眼PEP 8\n",
    "    print(\"Hello!\")\n",
    "    print(\"Hello, \"+username.title()+\"!\")\n",
    "greet_user('sarah')\n",
    "\n",
    "# 8.2 f(x,y)\n",
    "\n",
    "def describe_pet(animal_type, pet_name):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \"+animal_type+'.')\n",
    "    print(\"My \"+animal_type+\"'s name is \"+pet_name.title()+'.')\n",
    "\n",
    "describe_pet('hamster','harry')\n",
    "## 默认传递参数的顺序\n",
    "\n",
    "describe_pet('dog','whillie')\n",
    "## 函数嘛，定义一次之后，可以多次调用\n",
    "\n",
    "describe_pet(animal_type='hamester',pet_name='harry')\n",
    "## 可以指定给参数传递值，不用考虑顺序\n",
    "describe_pet(pet_name='harry',animal_type='hamester')\n",
    "\n",
    "# 设定参数默认值\n",
    "\n",
    "def describe_pet(pet_name, animal_type='dog'):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \"+animal_type+'.')\n",
    "    print(\"My \"+animal_type+\"'s name is \"+pet_name.title()+'.')\n",
    "\n",
    "describe_pet(pet_name='whillie')\n",
    "describe_pet(pet_name='yuanyuan',animal_type='panda')\n",
    "\n",
    "# 参数不对劲的时候，会报错\n",
    "# describe_pet()\n",
    "\n",
    "\n",
    "# 8.3 返回值\n",
    "\n",
    "## 返回一个字符串\n",
    "\n",
    "def get_formatted_name(first_name, last_name):\n",
    "    \"\"\"返回姓名\"\"\"\n",
    "    full_name= first_name+\" \"+last_name\n",
    "    return full_name.title()\n",
    "\n",
    "musician=get_formatted_name('jimi','hendrix')\n",
    "print(musician)\n",
    "\n",
    "## 也许有的人有中间名，有的人没有，可选择性输入的参数\n",
    "\n",
    "def get_formatted_name(first_name, last_name,middle_name=''):\n",
    "    \"\"\"返回姓名\"\"\"\n",
    "    if middle_name:\n",
    "        full_name= first_name+\" \"+middle_name+' '+last_name\n",
    "    else:\n",
    "        full_name = first_name + \" \" + last_name\n",
    "    return full_name.title()\n",
    "\n",
    "musician=get_formatted_name('jimi','hendrix')\n",
    "print(musician)\n",
    "musician=get_formatted_name('jimi','hendrix','lee')\n",
    "print(musician)\n",
    "\n",
    "# 返回字典\n",
    "\n",
    "def build_person(first_name, last_name, age=''):\n",
    "    person={'first':first_name,'last':last_name}\n",
    "    if age:\n",
    "        person['age']=age\n",
    "    return person\n",
    "musician=build_person('jimi','hendrix',age=27)\n",
    "print(musician)\n",
    "\n",
    "# 结合函数使用while循环\n",
    "\n",
    "def get_formatted_name(first_name, last_name):\n",
    "    full_name=first_name+' '+last_name\n",
    "    return full_name.title()\n",
    "\n",
    "while True:\n",
    "    print(\"\\nPlease tell me your name:\")\n",
    "    print(\"(enter 'q' at any time to quit)\")\n",
    "\n",
    "    f_name=input(\"First name:\")\n",
    "    if f_name=='q':\n",
    "        break\n",
    "\n",
    "    l_name=input(\"Last name:\")\n",
    "    if l_name=='q':\n",
    "        break\n",
    "\n",
    "    formatted_name=get_formatted_name(f_name,l_name)\n",
    "    print(\"\\nHello, \"+formatted_name+\"!\")\n",
    "\n",
    "# 8.4 传递列表，就是你要给变量传递的值有一排\n",
    "\n",
    "def greet_users(names):\n",
    "    \"\"\"向列表中的每位用户都发出简单的问候\"\"\"\n",
    "    for name in names:\n",
    "        msg=\"Hello, \"+name.title()+\"!\"\n",
    "        print(msg)\n",
    "\n",
    "usernames=['hanna','tw','we']\n",
    "greet_users(usernames)\n",
    "\n",
    "\n",
    "## 转移列表功能，抽象出来\n",
    "unprinted_designs=['a','b','c','d']\n",
    "completed_models=[]\n",
    "\n",
    "while unprinted_designs:\n",
    "    current_design=unprinted_designs.pop()\n",
    "\n",
    "    print(\"Printing model: \"+current_design)\n",
    "    completed_models.append(current_design)\n",
    "\n",
    "print(\"\\nThe following models have been printed:\")\n",
    "for completed_model in completed_models:\n",
    "    print(completed_model)\n",
    "\n",
    "## 用两个函数实现列表转移功能\n",
    "\n",
    "def print_models(unprinted_designs,completed_models):\n",
    "    while unprinted_designs:\n",
    "        current_design=unprinted_designs.pop()\n",
    "        print(\"Printing model: \"+current_design)\n",
    "        completed_models.append(current_design)\n",
    "\n",
    "def show_completed_models(completed_models):\n",
    "    print(\"\\nThe following models have been printed:\")\n",
    "    for completed_model in completed_models:\n",
    "        print(completed_model)\n",
    "\n",
    "# unprinted_designs=['a','b','c','d']\n",
    "# # 创建一个列表，其中包含一些要打印的信息\n",
    "# completed_models=[]\n",
    "\n",
    "# print_models(unprinted_designs,completed_models)\n",
    "# show_completed_models(completed_models)\n",
    "# show_completed_models(unprinted_designs)\n",
    "\n",
    "## 如果不想掏空原来的列表，只想复制，那用切片进行引用\n",
    "unprinted_designs=['iphone case','robot pendant','dodecahedron']\n",
    "completed_models=[]\n",
    "\n",
    "print_models(unprinted_designs[:],completed_models)\n",
    "show_completed_models(unprinted_designs)\n",
    "show_completed_models(completed_models)\n",
    "## 上面的操作要注释掉\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# 8.5 任意数量的实参\n",
    "def make_pizza(*toppings):\n",
    "    print('我要一个酱婶儿的披萨'+str(toppings))\n",
    "make_pizza('pepperoni')\n",
    "make_pizza(\"mushrooms\",'green peppers','extra cheese')\n",
    "\n",
    "#for循环引入任意数量实参\n",
    "def make_pizza(*toppings):\n",
    "    print(\"\\nMaking a pizza with the following toppings:\")\n",
    "    for topping in toppings:\n",
    "        print(\"- \"+topping)\n",
    "make_pizza('pepperoni')\n",
    "make_pizza(\"mushrooms\",'green peppers','extra cheese')\n",
    "\n",
    "#位置实参+任意数量实参,实际是元组参数\n",
    "def make_pizza(size,*toppings):\n",
    "    print('\\nMaking a '+str(size)+\"-inch pizza with the following toppings:\")\n",
    "    for topping in toppings:\n",
    "        print(\"- \"+topping)\n",
    "make_pizza(16,'pepperoni')\n",
    "make_pizza(12,'mushrooms','green peppers','extra cheese')\n",
    "\n",
    "#字典参数\n",
    "def build_profile(first, last, **user_info):\n",
    "    profile={}\n",
    "    profile['first_name']=first\n",
    "    profile['last_name']=last\n",
    "    for key, value in user_info.items():\n",
    "        profile[key]=value\n",
    "    return profile\n",
    "user_profile=build_profile('albert','einstein',location='princeton',field='physics')\n",
    "print(user_profile)\n",
    "\n",
    "# 8.6 将函数储存在模块中\n",
    "\n",
    "#调用函数\n",
    "\n",
    "import sys\n",
    "sys.path.append(\"D:\\Python练习\") ## 一定要有这个！！！\n",
    "\n",
    "import pizza\n",
    "pizza.make_pizza(16,'pepperoni')\n",
    "pizza.make_pizza(16,'pepperoni','green peppers','extra cheese')\n",
    "\n",
    "#调用特定函数\n",
    "import sys\n",
    "sys.path.append(\"D:\\Python练习\") ## 一定要有这个！！！\n",
    "\n",
    "from pizza import make_pizza as mp\n",
    "\n",
    "mp(16,'pepperoni')\n",
    "\n",
    "#给调用的模块指定名\n",
    "\n",
    "import sys\n",
    "sys.path.append(\"D:\\Python练习\") ## 一定要有这个！！！\n",
    "import pizza as p\n",
    "p.make_pizza(16,'pepperoni')\n",
    "\n",
    "#导入模块中所有函数\n",
    "import sys\n",
    "sys.path.append(\"D:\\Python练习\")\n",
    "\n",
    "from pizza import *\n",
    "make_pizza(16,'pepperoni')\n",
    "make_pizza(21,'mushrooms','green peppers','extra cheese')\n",
    "\n",
    "# # 8.7 函数编写指南，看眼书，PEP 8\n",
    "#\n",
    "\n",
    "a='nihao'\n",
    "help(a.title)\n",
    "dir\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 8-1 定义函数，调用函数\n",
    "\n",
    "def display_message():\n",
    "    print('颤抖吧函数，我来学你了哈哈哈哈！')\n",
    "\n",
    "display_message()\n",
    "\n",
    "# 8-2 喜欢的图书\n",
    "\n",
    "def favorite_book(book):\n",
    "    print(book+'这本书老好了，振聋发聩醍醐灌顶恍然大悟重新做人。')\n",
    "\n",
    "favorite_book('道德经')\n",
    "\n",
    "# 8-3 打印衬衫\n",
    "def make_shirt(word,size):\n",
    "    if size =='X':\n",
    "        print(word)\n",
    "        print('一起啃书')\n",
    "\n",
    "    if size =='M':\n",
    "        print(' -----_____----- ')\n",
    "        print('|               |')\n",
    "        print(' ---         ---')\n",
    "        print('   |'+word+' !|   ')\n",
    "        print('   |  一起啃书|   ')\n",
    "        print('   |   ❥(^_-)|   ')\n",
    "        print('    ---------    ')\n",
    "\n",
    "    if size=='L':\n",
    "        print(word)\n",
    "        print('一起啃书')\n",
    "\n",
    "make_shirt('奋发图强','X')\n",
    "make_shirt(size='M',word='誓死不屈')\n",
    "\n",
    "# 8-4\n",
    "\n",
    "def make_shirt(size,word='我爱拍桑'):\n",
    "    if size =='X':\n",
    "        print(word)\n",
    "        print('一起啃书')\n",
    "\n",
    "    if size =='M':\n",
    "        print(' -----_____----- ')\n",
    "        print('|               |')\n",
    "        print(' ---         ---')\n",
    "        print('   |'+word+' !|   ')\n",
    "        print('   |  一起啃书|   ')\n",
    "        print('   |   ❥(^_-)|   ')\n",
    "        print('    ---------    ')\n",
    "\n",
    "    if size=='L':\n",
    "        print(word)\n",
    "        print('一起啃书')\n",
    "\n",
    "make_shirt('M')\n",
    "make_shirt('M','冲鸭冲鸭')\n",
    "\n",
    "# 8-5\n",
    "def describe_city(city,country='China'):\n",
    "    print(city+'在'+country+'.')\n",
    "describe_city('Hangzhou')\n",
    "describe_city('zhuhai')\n",
    "describe_city('gongzhufen')\n",
    "describe_city('wangchuan','广莫之野无何有之乡')\n",
    "\n",
    "# 8-6 返回值\n",
    "\n",
    "def describe_city(city,country='China'):\n",
    "    city_country=city+','+country\n",
    "    return city_country\n",
    "\n",
    "city_country_liaoning=describe_city('Liaoning')\n",
    "\n",
    "print(city_country_liaoning)\n",
    "\n",
    "# 8-7 返回字典\n",
    "\n",
    "def make_album(singer,special_edition):\n",
    "    music={}\n",
    "    music['singer']=singer\n",
    "    music['special_edition']=special_edition\n",
    "    print(music)\n",
    "\n",
    "make_album('周杰伦','依然范特西')\n",
    "make_album('周杰伦','摩羯座')\n",
    "make_album('致敬大神','一起啃书')\n",
    "\n",
    "# 8-8 while\n",
    "\n",
    "def make_album(singer,special_edition):\n",
    "    music={}\n",
    "    music['singer']=singer\n",
    "    music['special_edition']=special_edition\n",
    "    print(music)\n",
    "\n",
    "print(\"输入你喜欢的歌手和专辑，输入't'随时退出\")\n",
    "while True:\n",
    "    singer=input('你喜欢的歌手是谁？')\n",
    "    if singer=='t':\n",
    "        break\n",
    "    else:\n",
    "        special_edition=input('他有什么专辑？')\n",
    "        if special_edition=='t':\n",
    "            break\n",
    "        else:\n",
    "            make_album(singer,special_edition)\n",
    "\n",
    "# 8-9 函数，功能是打印列表\n",
    "\n",
    "def print_book_list(book_list):\n",
    "    for book in book_list:\n",
    "        print(book)\n",
    "\n",
    "python_book_list=['Python编程从入门到实践','Python基础教程','Python语言及其应用']\n",
    "print_book_list(python_book_list)\n",
    "\n",
    "# 8-10 利用函数，对列表进行修改\n",
    "\n",
    "def print_book_list(book_list):\n",
    "    \"\"\"打印列表\"\"\"\n",
    "    for book in book_list:\n",
    "        print(book)\n",
    "\n",
    "def change_book_list(book_list):\n",
    "    \"\"\"修改列表\"\"\"\n",
    "    n=0\n",
    "    while n<len(book_list):\n",
    "        book_list[n]='推荐：'+book_list[n]\n",
    "        n+=1\n",
    "    print_book_list(book_list)\n",
    "\n",
    "\n",
    "python_book_list=['Python编程从入门到实践','Python基础教程','Python语言及其应用']\n",
    "\n",
    "change_book_list(python_book_list)\n",
    "\n",
    "# 8-11\n",
    "\n",
    "def print_book_list(book_list):\n",
    "    \"\"\"打印列表\"\"\"\n",
    "    for book in book_list:\n",
    "        print(book)\n",
    "\n",
    "def change_book_list(book_list):\n",
    "    \"\"\"修改列表\"\"\"\n",
    "    n=0\n",
    "    while n<len(book_list):\n",
    "        book_list[n]='推荐：'+book_list[n]\n",
    "        n+=1\n",
    "    print_book_list(book_list)\n",
    "\n",
    "\n",
    "python_book_list=['Python编程从入门到实践','Python基础教程','Python语言及其应用']\n",
    "\n",
    "change_book_list(python_book_list[:])\n",
    "print_book_list(python_book_list)\n",
    "\n",
    "\n",
    "# 8-12 输入任意数量实参 三明治里加什么\n",
    "\n",
    "\n",
    "def sandwich(*foods):\n",
    "    print('您的要的三明治：')\n",
    "    for food in foods:\n",
    "        print('-'+food)\n",
    "\n",
    "sandwich('guojiang','naiyou','huotui')\n",
    "\n",
    "# 8-13\n",
    "\n",
    "def build_profile(first, last, **user_info):\n",
    "    profile={}\n",
    "    profile['first_name']=first\n",
    "    profile['last_name']=last\n",
    "    for key, value in user_info.items():\n",
    "        profile[key]=value\n",
    "    return profile\n",
    "wo=build_profile('zhijing','dashen',tezheng='mei',aihao='zhaojingzi')\n",
    "print(wo)\n",
    "\n",
    "# 8-14 自己定义一个字典 照上面抄就可以了\n",
    "\n",
    "def cars(brand,type,**info):\n",
    "    vehicle={}\n",
    "    vehicle['牌子']=brand\n",
    "    vehicle['型号']=type\n",
    "    for key, value in info.items():\n",
    "        vehicle[key]=value\n",
    "    return vehicle\n",
    "\n",
    "che=cars('aima','diandongche',daiyan='zhoujielun',yanse='fen')\n",
    "print(che)\n",
    "\n",
    "# 8-15 实现的功能就是函数导入 8.4.1的函数，\n",
    "\n",
    "\n",
    "import sys\n",
    "sys.path.append(\"D:\\Python练习\")\n",
    "\n",
    "from printing_function import *\n",
    "\n",
    "unprinted_designs=['iphone case','robot pendant','dodecahedron']\n",
    "completed_models=[]\n",
    "\n",
    "# printing_function.print_models(unprinted_designs[:],completed_models)\n",
    "# printing_function.show_completed_models(unprinted_designs)\n",
    "# printing_function.show_completed_models(completed_models)\n",
    "\n",
    "\n",
    "print_models(unprinted_designs[:],completed_models)\n",
    "show_completed_models(unprinted_designs)\n",
    "show_completed_models(completed_models)\n",
    "\n",
    "\n",
    "\n",
    "# 至 8-17\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
